// pages/record/main/main.js
const app = getApp();
const model = require('../../../utils/model');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    status:'init',
    statusText: '',
    content:'',
    title:'',
    id:0
  },

  record: function () {
    var status = this.data.status, statusText;
    if (status == 'init') {
      status = 'play';
      statusText = '正在录制中...';
    }
    else if (status == 'play') {
      status = 'pause';
      statusText = '录制已暂停...';
    }
    else {
      status = 'play';
      statusText = '正在录制中...';
    }
    this.setData({
      status:status,
      statusText:statusText
    });
  },

  tapsave:function(){
    this.save(false);
  },

  save: function (callback) {
    var me = this;
    model.request({
      action: 'saveReading',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        id:this.data.id,
        mp3_url:''
      },
      success: function (res) {
        if (callback) {
          callback(res.data.id,me.data.title);
        }
        else {
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000
          });
        }
      }
    });
  },

  tappublish:function(){
    this.save(function (id,title) {
      wx.navigateTo({
        url:'/pages/works/publish/publish?id=' + id + "&title=" + title
      });
    });
  },

  getArticleDetail:function(id){
    var me = this;
    model.request({
      action:'mwpdDetail',
      data:{
        id:id
      },
      success: function (res) {
        //console.log(res.data.flash_list);
        me.setData({
          content:res.data.detail.info,
          title:res.data.detail.title,
          id:res.data.detail.id
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getArticleDetail(options.id);
    this.setData({
      scrollHeight: app.globalData.winHeight - 93 - 122
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})